﻿using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Threading;

namespace PlayListSet.Comm
{
    //public delegate void TimeCodePlaybackState(PlaybackState playbackState);
    //public delegate void TimeCodeChanged(TimeSpan currentTime);

    internal class TimeCodePlayContorl2 : System.ComponentModel.INotifyPropertyChanged,IDisposable
    {
        private System.Timers.Timer timer;
        private System.Timers.Timer timer2;

       
        private PlaybackState playbackState= PlaybackState.Stopped;
        public event TimeCodePlaybackState OnTimeCodePlaybackState;
        public event TimeCodeChanged TimeCodeChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        private TimeSpan currentTime;
        public TimeSpan CurrentTime
        {
            get { 
                return currentTime; 
            }
            set
            {
                currentTime = value;
            }
        }

        public TimeSpan GetCurrentTime()
        {
            return CurrentTime; 
        }
        public PlaybackState PlaybackState
        {
            set
            {
                playbackState = value; 
                OnTimeCodePlaybackState?.Invoke(value);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlaybackState"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlaybackStateInt"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlaybackStateStr"));

            }
            get { return playbackState; }
        }
        public string PlaybackStateStr
        {
            get
            {
                switch (PlaybackState)
                {
                    case PlaybackState.Stopped: return "停止";
                    case PlaybackState.Playing: return "正在播放中";
                    case PlaybackState.Paused: return "暂停";
                    default: return "停止";
                }
            }
        }
        public int PlaybackStateInt
        {

            get
            {
                switch (PlaybackState)
                {
                    case PlaybackState.Stopped: return 0;
                    case PlaybackState.Playing: return 1;
                    case PlaybackState.Paused: return 2;
                    default: return 0;
                }
            }
        }
        public TimeCodePlayContorl2( ) 
        {
            timer = new System.Timers.Timer(30);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();

            timer2 = new System.Timers.Timer(10);
            timer.Elapsed += Timer_Elapsed2;
            timer.Start();
        }

      
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if(PlaybackState== PlaybackState.Playing)
            {
                 TimeCodeChanged?.Invoke(CurrentTime);
            }
        }
        private double startTick = 0;
 
        private void Timer_Elapsed2(object sender, ElapsedEventArgs e)
        {
            switch (PlaybackState)
            {
                case PlaybackState.Stopped:
                    //CurrentTime = TimeSpan.Zero;
                    break;
                case PlaybackState.Playing: // 更新当前时间
                     CurrentTime = TimeSpan.FromMilliseconds(Environment.TickCount - startTick);
                    if (currentTime.TotalSeconds >= MainWindow.TimeCodeSeconds)
                        Stop();
                    break;
                case PlaybackState.Paused:
                     
                    break;
                default:
                    break;
            }
        }

        

        public void Play()
        {
            if (PlaybackState == PlaybackState.Stopped)
            {
                startTick = System.Environment.TickCount - CurrentTime.TotalMilliseconds;
            }
            else if(PlaybackState == PlaybackState.Paused)
            {
                startTick = System.Environment.TickCount-CurrentTime.TotalMilliseconds;
            }
            PlaybackState =  PlaybackState.Playing;
        }
        public void Pause()
        {
            if(playbackState == PlaybackState.Playing)
            {
                 PlaybackState = PlaybackState.Paused;
            }
           
        }
        public void Stop()
        {
            PlaybackState = PlaybackState.Stopped;
            CurrentTime = TimeSpan.Zero;
            TimeCodeChanged?.Invoke(CurrentTime);
        }
        public void Seek(TimeSpan duration)
        {
            startTick = System.Environment.TickCount - duration.TotalMilliseconds;
            CurrentTime = TimeSpan.FromMilliseconds(Environment.TickCount - startTick);
            TimeCodeChanged?.Invoke(CurrentTime);
        }

        public void Dispose()
        {
            Stop();
            timer2.Stop();
            timer.Stop();
        }

        
    }
}
